//
//  ProblemSearchConditionViewController.m
//  Utils
//
//  Created by enghou on 2018/4/14.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "ProblemSearchConditionViewController.h"
#import "ProblemsHandlingViewController.h"
#import "NetWorkerManager.h"
#import "QueryTypeSelectionController.h"
@interface ProblemSearchConditionViewController ()<HandleType>


//@property(nonatomic,strong)UIDatePicker *date;

//@property(nonatomic,strong)UIDatePicker *endDate;

//@property(nonatomic,strong)UITextField *beginField;

//@property(nonatomic,strong)UITextField *endField;

@end

@implementation ProblemSearchConditionViewController
- (IBAction)showBeginTime:(id)sender {
    [self.beginField becomeFirstResponder];
}
- (IBAction)showEndTime:(id)sender {
    [self.endField becomeFirstResponder];;
}

-(void)sureAction{
    if ([self.beginField isFirstResponder]) {
        self.beginTimeField.text = [[self.date date]dateLineStrOnlyDay];
    }else if ([self.endField isFirstResponder]){
        self.endTimeField.text = [[self.endDate date]dateLineStrOnlyDay];
    }
    [self.view endEditing:YES];
}

-(void)cancelAction{
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"矛盾纠纷查询";
    self.beginField = [[UITextField alloc]init];
    [self.view addSubview:self.beginField];
    self.date = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    self.date.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    self.date.datePickerMode = UIDatePickerModeDate;
    self.date.maximumDate = [NSDate date];
    self.date.backgroundColor = [UIColor whiteColor];
    self.beginField.inputView = self.date;
    self.beginField.inputAccessoryView = [self inputAccessView];
    
    self.endField = [[UITextField alloc]init];
    [self.view addSubview:self.endField];
    self.endDate = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    self.endDate.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    self.endDate.datePickerMode = UIDatePickerModeDate;
    self.endDate.maximumDate = [NSDate date];
    self.endDate.backgroundColor = [UIColor whiteColor];
    self.endField.inputView = self.endDate;
    self.endField.inputAccessoryView = [self inputAccessView];
}

-(void)scaleAlert{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    for (PropertyDictItemModel *model in self.eventScale) {
        UIAlertAction *act = [UIAlertAction actionWithTitle:model.displayName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.scaleId = model.ids;
            self.eventScaleLabel.text = model.displayName;
        }];
        [alert addAction:act];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)showScales{
    if (!self.eventScale) {
        [self beginLoading];
        [[NetWorkerManager sharedInstance]getPropertyDictWithDomainName:@"事件性质" successBlock:^(BaseModel *model) {
            [self endLoading];
            if (model.success) {
                self.eventScale = model.list;
                [self scaleAlert];
            }
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }else{
        [self scaleAlert];
    }
}

-(void)sourcesAlert{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    for (PropertyDictItemModel *model in self.eventSource) {
        UIAlertAction *act = [UIAlertAction actionWithTitle:model.displayName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.sourceId = model.ids;
            self.sourceLabel.text = model.displayName;
        }];
        [alert addAction:act];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)showSources{
    if (!self.eventSource) {
        [self beginLoading];
        [[NetWorkerManager sharedInstance]getPropertyDictWithDomainName:@"事件来源方式" successBlock:^(BaseModel *model) {
            [self endLoading];
            if (model.success) {
                self.eventSource = model.list ;
                [self sourcesAlert];
            }else{
                [self alertSome:model.msg];
            }
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }else{
        [self sourcesAlert];
    }
}

-(void)selectEventType{
    QueryTypeSelectionController *type = (QueryTypeSelectionController *)[VCLoader vcWithName:@"QueryTypeSelectionController"];
    type.theDelete = self;
    type.itemCls = @"TypeListModel";
    type.path = kProblemEventType;
    [self push:type];
}

-(void)queryAction{
    ProblemsHandlingViewController *problem = (ProblemsHandlingViewController *)self.theDelete;
    problem.eventName = self.eventNameField.text;
    problem.scaleId = self.scaleId;
    problem.eventSource = self.sourceId;
    problem.typeId = self.typeId;
    problem.beginTime = self.beginTimeField.text;
    problem.endTime = self.endTimeField.text;
    [problem beginRefresh];
    [self pop];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 1:
            [self showScales];
            break;
        case 2:
            [self showSources];
            break;
        case 3:
            [self selectEventType];
            break;
        default:
            break;
    }
}

#pragma <HandleType> Delegate
-(void)handleSubModel:(SubTypeItemModel *)model{
    self.eventTypeField.text = model.name;
    self.typeId = model.subTid;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
